/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlykho;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Phuong Nam
 */
public class ViewDeleteUser extends javax.swing.JPanel {

    /**
     * Creates new form ViewDeleteUser
     */
    DefaultTableModel model;
    public ViewDeleteUser() throws SQLException {
         initComponents();
            model = (DefaultTableModel) jt1.getModel();
            
           
                    Ketnoi a = new Ketnoi();
                    Connection con;
                    con = a.connect();
                    PreparedStatement pst = con.prepareCall("Select * from dangnhap");
                    ResultSet rs = pst.executeQuery();
                    Vector v = null;
                    while(rs.next())
                    {
                        v = new Vector();
                        v.add(rs.getString(1));
                        v.add(rs.getString(2));
                        v.add(rs.getString(3));
                        model.addRow(v);
                    }
                    con.close();
        
    }
    /**
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Delete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt1 = new javax.swing.JTable();

        Delete.setText("Delete");
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });

        jt1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "User", "Password", "Ưu Tiên"
            }
        ));
        jScrollPane1.setViewportView(jt1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(154, 154, 154)
                .addComponent(Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Delete)
                .addContainerGap(16, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
        try {       
            // TODO add your handling code here:
            Ketnoi a = new Ketnoi();
            Connection con;
            con = a.connect();

            PreparedStatement pstmt = null;
            try {           
                pstmt = con.prepareStatement("DELETE FROM dangnhap WHERE [User]=?");
            } catch (SQLException ex) {
                Logger.getLogger(ViewDeleteUser.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            String user = jt1.getValueAt(jt1.getSelectedRow(),0).toString();
            pstmt.setString(1, user);
            pstmt.executeUpdate();
            model.removeRow(jt1.getSelectedRow());
            JOptionPane.showMessageDialog(null, "Delete successfully");
        } catch (SQLException ex) {
            Logger.getLogger(ViewDeleteUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        

    }//GEN-LAST:event_DeleteActionPerformed
       
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Delete;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jt1;
    // End of variables declaration//GEN-END:variables
}
